package com.koo.modules.quartz.server.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.koo.common.exception.RRException;
import com.koo.common.utils.MyDateUtil;
import com.koo.common.utils.MyStrUtil;
import com.koo.modules.cx.entity.CxInspectionOrderDetailEntity;
import com.koo.modules.cx.entity.CxInspectionOrderEntity;
import com.koo.modules.cx.entity.CxOrderOutEntity;
import com.koo.modules.cx.entity.CxStoreInfoEntity;
import com.koo.modules.cx.service.*;
import com.koo.modules.quartz.constant.B2BConstant;
import com.koo.modules.quartz.server.BaseThirdDataService;
import com.koo.modules.quartz.util.ChangeUtil;
import com.koo.modules.quartz.util.SqlJDBCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Component
public class CXThirdDataServiceImpl implements BaseThirdDataService {

    @Autowired
    private CxStoreInfoService cxStoreInfoService;
    @Autowired
    private CxProductService cxProductService;
    @Autowired
    private CxOrderOutService cxOrderOutService;

    @Autowired
    private CxInspectionOrderService cxInspectionOrderService;
    @Autowired
    private CxInspectionOrderDetailService cxInspectionOrderDetailService;


    @Override
    public void storeDataTransport() {
        if (!checkNeedThirdData()) {
            return;
        }
        Date maxCreateDate = cxStoreInfoService.selectMaxCreateDate();
        String addDate = null;
        if (MyStrUtil.isNotEmpty(maxCreateDate)) {
            addDate = MyDateUtil.formatCurrDateTime();
        }
        String sql = B2BConstant.CX_PULL_STORE_SQL.getSql();
        if (StringUtil.isNotEmpty(addDate)) {
            sql = sql.replace("{WHERE_LIST}", " WHERE CONVERT (VARCHAR(25), addTime, 120) >= '" + addDate + "' ");
        } else {
            sql = sql.replace("{WHERE_LIST}", " ");
        }
        Connection connection = null;
        List<CxStoreInfoEntity> infos = new ArrayList<>();
        try {
            connection = SqlJDBCUtil.getConnection(B2BConstant.CX.getCompanyCode());
            ResultSet rs = SqlJDBCUtil.execQuery(sql, connection);
            infos = ChangeUtil.changeResultToList(rs, CxStoreInfoEntity.class);
        } finally {
            SqlJDBCUtil.commit(connection);
        }
        if (infos.isEmpty()) {
            return;
        }
        cxStoreInfoService.saveOrUpdateBatch(infos);
    }

    @Override
    public void productDataTransport() {
        //从出库单中转换商品信息
    }

    @Override
    public void outOrderDataTransport() {
        if (!checkNeedThirdData()) {
            return;
        }
        String maxOrderId = cxOrderOutService.selectMaxOrderId();
        //insert into cxBaseOrderOut(orderOutId,headQuarterAllotDate,headQuarterAllotNo,remark,state) values(UUID_SHORT(), '2020-08-01 00:00:00', 0, '基础数据', 1);
        if (StringUtil.isEmpty(maxOrderId)) {
            throw new RRException("同步出库单时出库单号不能为空,否则查询全部数据!!!");
        }
        String sql = B2BConstant.CX_PULL_OUT_ORDER_SQL.getSql().replace("{WHERE_LIST}", "WHERE 订单id > " + maxOrderId + " ");
        Connection connection = null;
        List<CxOrderOutEntity> orderOuts = new ArrayList<>();
        try {
            connection = SqlJDBCUtil.getConnection(B2BConstant.CX.getCompanyCode());
            ResultSet resultSet = SqlJDBCUtil.execQuery(sql, connection);
            orderOuts = ChangeUtil.changeResultToList(resultSet, CxOrderOutEntity.class);
        } finally {
            SqlJDBCUtil.commit(connection);
        }
        if (orderOuts.isEmpty()) {
            return;
        }
        this.updateSaleToProduct(orderOuts);
        //
        cxOrderOutService.insertByList(orderOuts, maxOrderId);
    }


    @Override
    public void uploadMemberData() {

    }

    @Override
    public void uploadSaleData() {

    }

    @Override
    public void uploadStockData() {

    }

    @Override
    public boolean checkNeedThirdData() {
        return ChangeUtil.checkNeedThridDataService(B2BConstant.CX.getCompanyCode());
    }

    @Override
    public void convertInspectionOrderData() {
        if (!checkNeedThirdData()) {
            return;
        }
        List<CxInspectionOrderEntity> outOrders = cxInspectionOrderService.listOutOrders(B2BConstant.MAX_CONVERT_OUT_ORDER_LIMIT_SIZE.getNumber());
        List<CxInspectionOrderDetailEntity> orderDetails = new ArrayList<>();
        Iterator<CxInspectionOrderEntity> iterator = outOrders.iterator();
        while (iterator.hasNext()) {
            CxInspectionOrderEntity order = iterator.next();
            boolean hasOrder = cxInspectionOrderService.findHasOrderByHeadNo(order.getHeadQuarterAllotNo());
            if (hasOrder) {
                iterator.remove();
            } else {
                //查询明细,添加明细到待插入列表.
                List<CxInspectionOrderDetailEntity> listDetails = cxInspectionOrderDetailService.listInspectionOrderDetails(order.getHeadQuarterAllotNo());
                orderDetails.addAll(listDetails);
            }
        }
        if (outOrders.isEmpty()) {
            return;
        }
        cxInspectionOrderService.insertByListAndUpdateState(outOrders, orderDetails);
    }

    @Override
    public void storeDataToAdminTransport() {

    }

    /**
     * 从出库单中转换商品信息
     *
     * @param orderOuts
     * @return
     */
    private Integer updateSaleToProduct(List<CxOrderOutEntity> orderOuts) {
        return cxProductService.updateSaleToProduct(orderOuts);
    }
}
